package com.jtp7.demo.controller;


import com.jtp7.demo.config.SwaggerConfiguration;
import com.jtp7.demo.entity.TruckInfo;
import com.jtp7.demo.entity.mapstruct.TruckInfoMapsTruct;
import com.jtp7.demo.entity.response.PageListResult;
import com.jtp7.demo.entity.vo.AddTruckInfoDTO;
import com.jtp7.demo.entity.response.CommonCode;
import com.jtp7.demo.entity.response.ResponseResult;
import com.jtp7.demo.entity.dto.TruckInfoDTO;
import com.jtp7.demo.service.ITruckInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2021-07-27
 */
@Slf4j
@RestController
@CrossOrigin(allowedHeaders = "*")
@Api(value = "司机信息表", tags = {SwaggerConfiguration.TAG_1})
//@Secured({"ROLE_manager"}) 权限控制
public class TruckInfoController {

    @Autowired
    private ITruckInfoService iTruckInfoService;


    @GetMapping("/TruckInfo/{id}")
    @ApiOperation(value = "根据id查询司机信息")
    @ApiImplicitParam(name = "id", value = "用户的id", required = true, paramType = "path", dataType = "String")
    @Cacheable(value = "id",key = "#id")
    public ResponseResult<TruckInfo> findById(@PathVariable("id") String id) {
        TruckInfo truckInfo = iTruckInfoService.getById(id);
        return new ResponseResult<>(CommonCode.SUCCESS, truckInfo);
    }

    @PostMapping("addTruckInfo")
    @ApiOperation(value = "新增司机,可以是批量,集合,数据不能超过200条 格式: [{....},{....}] ,返回的数据是已经注册的司机")
    @ApiImplicitParam(name = "addTruckInfoDTOList", value = "新增司机信息", required = true, paramType = "body", dataType = "List<AddTruckInfoDTO>")
    public ResponseResult<String> addTruckInfo(@RequestBody List<AddTruckInfoDTO> addTruckInfoDTOList) {

        //add条件
        Map<Object, Object> objectObjectMap = iTruckInfoService.addCondition(addTruckInfoDTOList);
        List<TruckInfo> truckInfoList =( List<TruckInfo>) objectObjectMap.get("1");
        List<String> names =(List<String>) objectObjectMap.get("2");

            iTruckInfoService.saveBatch(truckInfoList);

            //加入缓存
        iTruckInfoService.cacheRedis(truckInfoList);


        return new ResponseResult<>(CommonCode.YES_ADD_TRUCKINFO,names);
    }

    @PutMapping("updateTruckInfo")
    @ApiOperation(value = "修改司机信息")
    @ApiImplicitParam(name = "truckInfoDTO", value = "修改司机信息", required = true, paramType = "body", dataType = "TruckInfoDTO")
    public ResponseResult<Object> updateTruckInfo(@RequestBody TruckInfoDTO truckInfoDTO) {
        iTruckInfoService.update(truckInfoDTO);
        return new ResponseResult<>(CommonCode.YES_ADD_TRUCKINFO);
    }

    @GetMapping("/selectTruckInfoByName")
    @ApiOperation(value = "根据姓名查询司机信息,没有传参默认查询所有")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "用户的name,可以不传", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "currentPage", value = "当前的页码,默认1", required = false, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "size", value = "没有显示的条数,默认8条", required = false, paramType = "query", dataType = "Integer")
    })
    public ResponseResult<PageListResult<Object>> findByName(@RequestParam(value = "name", required = false) String name,
                                                                @RequestParam(value = "phone", required = false) String phone,
                                                                @RequestParam(value = "drivingCode", required = false) String drivingCode,
                                                                @RequestParam(value = "currentPage", required = false) Integer currentPage,
                                                                @RequestParam(value = "size", required = false) Integer size) {
        PageListResult<Object> pageListResult = iTruckInfoService.findByName(name,phone,drivingCode, currentPage, size);
        return new ResponseResult<>(CommonCode.SUCCESS, pageListResult);
    }


    @ApiOperation(value = "删除司机信息,可以批量删除 格式 [1,2,3,4]")
    @ApiImplicitParam(name = "id", value = "删除司机信息", required = true, paramType = "body", dataType = "String[]")
    @DeleteMapping("/deleteByIds")
    @CacheEvict(value = "id",allEntries = true)
    public ResponseResult<Object> deleteTruckInfo(@RequestBody String[] id) {
        boolean b = iTruckInfoService.removeByIds(Arrays.asList(id));
        if (b) {
            return new ResponseResult<>(CommonCode.SUCCESS);
        }
       return new ResponseResult<>(CommonCode.DELTE_TRUCKINFO_NO);
    }


    @ApiOperation(value = "根据状态查询司机信息")
    @ApiImplicitParam(name = "id", value = "根据状态查询司机信息", required = false, paramType = "query", dataType = "Integer")
    @GetMapping("/findByVersion")
    public ResponseResult<TruckInfo> findByVersion(@RequestParam(value = "id", required = false) Integer id) {
        if (ObjectUtils.isEmpty(id)) {
            id = 0;
        }
        List<TruckInfo> byVersion = iTruckInfoService.findByVersion(id);
        return new ResponseResult<>(CommonCode.SUCCESS, byVersion);
    }


}
